package com.matt.docpro.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.matt.docpro.config.Utils;
import com.matt.docpro.model.*;
import com.matt.docpro.service.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * AppointmentController
 * Created by on 2024/02/20.
 */
@Controller
public class AppointmentController {

    @Autowired
    AppointmentService appointmentService;
    @Autowired
    PatientService patientService;
    @Autowired
    DoctorService doctorService;
    @Autowired
    ClinicService clinicService;
    @Autowired
    LoginInfoService loginInfoService;
    @Autowired
    OutcomeService outcomeService;
    @Autowired
    UserService userService;

    @RequestMapping("/book-appointment")
    public String bookAppointment(Model model, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Patient patient = patientService.getById(user.getId());
//        List<Doctor> doctors = doctorService.getAll();
        List<Clinic> clinics = clinicService.getAll();
        model.addAttribute("patient", patient);
//        model.addAttribute("doctors", doctors);
        model.addAttribute("clinics", clinics);
        model.addAttribute("user", user);
        Map<Integer, List<String>> dateMap = new LinkedHashMap<>();
        DateTime today = DateUtil.date();
        String todayStr = DateUtil.format(today, "yyyy/MM/dd");
        String todayShowStr = DateUtil.format(today, "MM-dd")+" 当日号";
        List<String> dateStrList = new ArrayList<>();
        dateStrList.add(todayStr);
        dateStrList.add(todayShowStr);
        dateMap.put(1, dateStrList);
        for (int i = 1; i < 5; i++) {
            DateTime nextDay = DateUtil.offsetDay(today, i);
            String nextDayStr = DateUtil.format(nextDay, "yyyy/MM/dd");
            String nextDayShowStr = DateUtil.format(nextDay, "MM-dd")+" "+Utils.getWeekDay(nextDay);
            List<String> nextDateStrList = new ArrayList<>();
            nextDateStrList.add(nextDayStr);
            nextDateStrList.add(nextDayShowStr);
            dateMap.put(i+1, nextDateStrList);
        }
        model.addAttribute("dateMap", dateMap);

        return "book-appointment";
    }

    @RequestMapping("/book-appointment/getDocData")
    public String getDocData(Model model, String date, String clinicId, HttpServletRequest request) {
        List<Doctor> doctors = outcomeService.getOutcomeDocByDate(date, clinicId);
        for (Doctor doctor : doctors) {
            int appointmentCount = appointmentService.getAppointmentCount(date, clinicId, doctor.getId(), doctor.getStage());
            doctor.setAppCount(appointmentCount);
        }
        model.addAttribute("doctors", doctors);
        return "book-appointment::doc-info";
    }

    @RequestMapping("/book-appointment/getAppData")
    public String getAppData(Model model, String date, String clinicId, String docId, Integer stage, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        List<String> periodIdList = new ArrayList<>();
        List<String> periodList = new ArrayList<>();
        if (stage.equals(0)) {
            int periodCnt1 = appointmentService.getAppointmentPeriodCount(date, clinicId, docId, stage, 0);
            int periodCnt2 = appointmentService.getAppointmentPeriodCount(date, clinicId, docId, stage, 1);
            int periodCnt3 = appointmentService.getAppointmentPeriodCount(date, clinicId, docId, stage, 2);
            periodList.add("08:00-09:00 余号:" + (10-periodCnt1));
            periodList.add("09:00-10:00 余号:" + (10-periodCnt2));
            periodList.add("10:00-11:00 余号:" + (10-periodCnt3));
            periodIdList.add("0");
            periodIdList.add("1");
            periodIdList.add("2");
        } else if (stage.equals(1)) {
            int periodCnt4 = appointmentService.getAppointmentPeriodCount(date, clinicId, docId, stage, 3);
            int periodCnt5 = appointmentService.getAppointmentPeriodCount(date, clinicId, docId, stage, 4);
            int periodCnt6 = appointmentService.getAppointmentPeriodCount(date, clinicId, docId, stage, 5);
            periodList.add("13:00-14:00 余号:" + (10-periodCnt4));
            periodList.add("14:00-15:00 余号:" + (10-periodCnt5));
            periodList.add("15:00-16:00 余号:" + (10-periodCnt6));
            periodIdList.add("3");
            periodIdList.add("4");
            periodIdList.add("5");
        } else if (stage.equals(2)) {
            int periodCnt7 = appointmentService.getAppointmentPeriodCount(date, clinicId, docId, stage, 6);
            periodList.add("17:00-22:00 余号:" + (30-periodCnt7));
            periodIdList.add("6");
        } else if (stage.equals(3)) {
            int periodCnt8 = appointmentService.getAppointmentPeriodCount(date, clinicId, docId, stage, 7);
            periodList.add("23:00-次日07:00 余号:" + (30-periodCnt8));
            periodIdList.add("7");
        }
        model.addAttribute("periodList", periodList);
        model.addAttribute("periodIdList", periodIdList);
        return "book-appointment::period";
    }


    @PostMapping("/book-appointment/save")
    @ResponseBody
    public Map<String, Object> insertAppointment(Model model, Appointment appointment, HttpServletRequest request) {
        String token = Utils.getToken(request);
        User user = loginInfoService.getUserByToken(token);
        Map<String, Object> res = new HashMap<>();
//        appointment.setStateId("1");
        appointment.setId("A"+System.currentTimeMillis());
        appointment.setPatientId(user.getId());
        appointmentService.insert(appointment);
        if (appointment.getStateId().equals("1")) {
            res.put("state", "success");
        } else {
            res.put("state", "fail");
        }
        if (appointmentService.getAppointmentByPatIdAndNgState(user.getId()) >= 3) {
            userService.updateBaned(user.getId());
            res.put("state", "baned");
        }

        return res;
    }

}
